package servlet;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Trade;

import org.w3c.dom.Document;

import persistence.AddTrade;
import persistence.CustomerDao;
import persistence.TradeDao;

/**
 * Servlet implementation class PostContent
 */
@WebServlet("/PostContent")
public class PostContent extends MyHttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		process(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		process(request, response);
	}
	
	//TODO Connect the Database & Remove temp implement
	private void process(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			Document xmlDoc = getDocument(request);
			String username = xmlDoc.getElementsByTagName("username").item(0).getFirstChild().getNodeValue();
			
			printWriter = response.getWriter();
			response.setContentType("application/xml");
			printWriter.write("<content>");

//			//TEMP IMPLEMENT
//			printWriter.write("<entry>");
//			addTag("id", "547896321");
//			addTag("date", "2013-02-08");
//			addTag("title", "Linear Algebra 1000 Exercise");
//			addTag("author", "etenaelta");
//			addTag("image","userimages\\8919982331744603747.jpg");
//			addTag("type", "course");
//			addTag("status", "status_yes");
//			addTag("collect", "collectButton_no");
//			printWriter.write("</entry>");
//			
//			printWriter.write("<entry>");
//			addTag("id", "547896321");
//			addTag("date", "2013-02-08");
//			addTag("title", "A Life With Linear Algebra 1000 Exercise");
//			addTag("author", "etenaelta");
//			addTag("image","");
//			addTag("type", "life");
//			addTag("status", "status_yes");
//			addTag("collect", "collectButton_no");
//			printWriter.write("</entry>");
//			
//			printWriter.write("<entry>");
//			addTag("id", "547896321");
//			addTag("date", "2013-02-08");
//			addTag("title", "Linear Algebra 1000 Exercises");
//			addTag("author", "etenaelta");
//			addTag("image","");
//			addTag("type", "activity");
//			addTag("status", "status_yes");
//			addTag("collect", "collectButton_no");
//			printWriter.write("</entry>");
//			
//			printWriter.write("<entry>");
//			addTag("id", "547896321");
//			addTag("date", "2013-02-08");
//			addTag("title", "Magic Pencil For Linear Algebra 1000 Exercise");
//			addTag("author", "etenaelta");
//			addTag("image","");
//			addTag("type", "other");
//			addTag("status", "status_yes");
//			addTag("collect", "collectButton_no");
//			printWriter.write("</entry>");
//			
//			printWriter.write("<entry>");
//			addTag("id", "547896321");
//			addTag("date", "2013-02-08");
//			addTag("title", "Linear Algebra 1000 Exercise --Back Again!!!");
//			addTag("author", "etenaelta");
//			addTag("image","");
//			addTag("type", "course");
//			addTag("status", "status_yes");
//			addTag("collect", "collectButton_no");
//			printWriter.write("</entry>");
//			//TEMP IMPLEMENT END
			
			List<Trade> tradeList = AddTrade.getAllTrade();
			
			Iterator<Trade> it = tradeList.iterator();
			
			while(it.hasNext()) {
				printWriter.write("<entry>");
				Trade buffer = it.next();
				addTag("id", String.valueOf(buffer.getId()));
				addTag("date", buffer.getDate().toString());
				addTag("title", buffer.getTitle());
				addTag("author", buffer.getOwner().getName());
				addTag("image",buffer.getOwner().getStatus());
				addTag("type", buffer.getCategory().getName());
				
				//change to real function later
				
				addTag("status", TradeDao.isFinish(buffer.getId()) ? "status_no" : "status_yes");//status_yes --- not completed	status_no --- completed
				System.out.println(buffer.getId()+"		"+TradeDao.isFinish(buffer.getId()));
				addTag("collect", TradeDao.isFavorite(CustomerDao.nameToID(username),buffer.getId()) ? "collectButton_yes" : "collectButton_no");
				printWriter.write("</entry>");
			}
			printWriter.write("</content>");
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
